<?
require_once(dirname(__FILE__) . '/../../classes/report_interface.inc.php');

abstract class LoggerModuleGeoReportBaseClass 
{
    protected $database;

    public function __construct(LoggerDatabase $database) 
    {
        $this->database = $database;
    }

    protected function GetCountriesData($date1, $date2, $start, $count)
    {
        $date1 = '\'' . mysql_escape_string($date1) . '\'';
        $date2 = '\'' . mysql_escape_string($date2) . '\'';

        $SelectSQL = 'SELECT `country`, `s`.`string` AS `countryStr`, SUM(`summ`) AS `summ` FROM (';
        $SelectSQL .= 'SELECT `g`.`country`, SUM(`g`.`count`) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_geo_archive` `g` ';
        $SelectSQL .= 'WHERE `g`.`date`>='.$date1.' AND `g`.`date`<='.$date2.' ';
        $SelectSQL .= 'GROUP BY `g`.`country` ';

        $SelectSQL .= 'UNION ALL ';

        $SelectSQL .= 'SELECT `g`.`country`, COUNT(*) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_geo` `g` ';
        $SelectSQL .= 'WHERE `g`.`date`>='.$date1.' AND `g`.`date`<='.$date2.' ';
        $SelectSQL .= 'GROUP BY `g`.`country` ';

        $SelectSQL .= ') AS `__temp_query` ';
        $SelectSQL .= 'LEFT JOIN `[PREF]module_geo_strings` `s` ON `s`.`id`=`__temp_query`.`country` ';
        $SelectSQL .= 'GROUP BY `country` ';
        $SelectSQL .= 'ORDER BY `summ` DESC ';
        $SelectSQL .= 'LIMIT '.$start.','.$count;

        $data = array();
        $result = $this->database->Query($SelectSQL);
        if (!$result)
             trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        while ($row = $this->database->FetchAssoc($result))
        {
            $data[] = $row;
        }
        $this->database->FreeResult($result);

        return $data;
    }

    protected function GetCitiesData($date1, $date2, $country, $start, $count)
    {
        $date1 = '\'' . mysql_escape_string($date1) . '\'';
        $date2 = '\'' . mysql_escape_string($date2) . '\'';
        $country = '\'' . mysql_escape_string($country) . '\'';

        $SelectSQL = 'SELECT `city`, `s`.`string` AS `cityStr`, SUM(`summ`) AS `summ` FROM (';
        $SelectSQL .= 'SELECT `g`.`city`, SUM(`g`.`count`) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_geo_archive` `g` ';
        $SelectSQL .= 'WHERE `g`.`date`>='.$date1.' AND `g`.`date`<='.$date2.' AND `g`.`country`='.$country.' ';
        $SelectSQL .= 'GROUP BY `g`.`city` ';

        $SelectSQL .= 'UNION ALL ';

        $SelectSQL .= 'SELECT `g`.`city`, COUNT(*) AS `summ` ';
        $SelectSQL .= 'FROM `[PREF]module_geo` `g` ';
        $SelectSQL .= 'WHERE `g`.`date`>='.$date1.' AND `g`.`date`<='.$date2.' AND `g`.`country`='.$country.' ';
        $SelectSQL .= 'GROUP BY `g`.`city` ';

        $SelectSQL .= ') AS `__temp_query` ';
        $SelectSQL .= 'LEFT JOIN `[PREF]module_geo_strings` `s` ON `s`.`id`=`__temp_query`.`city` ';
        $SelectSQL .= 'GROUP BY `city` ';
        $SelectSQL .= 'ORDER BY `summ` DESC ';
        $SelectSQL .= 'LIMIT '.$start.','.$count;

        $data = array();
        $result = $this->database->Query($SelectSQL);
        if (!$result)
             trigger_error('Database Error: ' . $this->database->Error(), E_USER_ERROR);

        while ($row = $this->database->FetchAssoc($result))
        {
            $data[] = $row;
        }
        $this->database->FreeResult($result);

        return $data;
    }
}


class LoggerModuleGeoReport extends LoggerModuleGeoReportBaseClass implements LoggerReportInterface
{
    public function __construct(LoggerDatabase $database) 
    {
        parent::__construct($database);
    }

    public function GetReport($root, $date1, $date2, $params = false)
    {
        $limit_start = 0;
        $limit_count = 100;

        $xml = $root->ownerDocument;

        $name = $xml->createElement('report_name', 'География посетителей');
        $root->appendChild($name);

        $data = $this->GetCountriesData($date1, $date2, $limit_start, $limit_count);
        $reportData = $xml->createElement('report_data');
        $root->appendChild($reportData);

        foreach($data as $row)
        {
            $dataRow = $xml->createElement('row');
            $reportData->appendChild($dataRow);
            $dataRow->setAttribute('country', $row['country']);
            $dataRow->setAttribute('countryStr', $row['countryStr']);
            $dataRow->setAttribute('summ', $row['summ']);
        }

        $xsl = dirname(__FILE__) . '/templates/countries.xsl';
        $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$xsl.'"');
        $xml->insertBefore($pi, $root);
        return true;
    }
}


class LoggerModuleGeoExpandReport extends LoggerModuleGeoReportBaseClass implements LoggerReportInterface
{
    public function __construct(LoggerDatabase $database) 
    {
        parent::__construct($database);
    }

    public function GetReport($root, $date1, $date2, $params = false)
    {
        $limit_start = 0;
        $limit_count = 100;
        
        $country = false;
        if (isset($params['country']) && $params['country'] != '')
        {
            $country = $params['country'];
        }

        $xml = $root->ownerDocument;

        $data = $this->GetCitiesData($date1, $date2, $country, $limit_start, $limit_count);

        $reportData = $xml->createElement('report_data');
        $root->appendChild($reportData);

        foreach($data as $row)
        {
            $dataRow = $xml->createElement('row');
            $reportData->appendChild($dataRow);
            $dataRow->setAttribute('country', $country);
            $dataRow->setAttribute('city', $row['city']);
            $dataRow->setAttribute('cityStr', $row['cityStr']);
            $dataRow->setAttribute('summ', $row['summ']);
        }

        $xsl = dirname(__FILE__) . '/templates/cities.xsl';
        $pi = $xml->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="'.$xsl.'"');
        $xml->insertBefore($pi, $root);
        return true;
    }
}
